package cn.wolfcode.trip.admin.controller;


import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 游记资源
 */
@Controller
@RequestMapping("travel")
public class travelController {
    @Autowired
    private ITravelService travelService;

    /**
     * 待审核游记列表
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") TravelQueryObject qo, Model model){
        //1.根据更新时间排列
        qo.setOrderBy("t.lastUpdateTime asc");
        //2.待审核状态才查询出来<TravelQueryObject加一个属性>
        if(qo.getState()==null){
            qo.setState(Travel.STATE_AUDIT);
        }

        //3.公开的游记
        qo.setIsPublic(true);
        model.addAttribute("pageInfo",travelService.query(qo));
        return "/travel/list";
    }


    /**
     * 已发布的游记
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/releaseList")
    public String releaseList(@ModelAttribute("qo") TravelQueryObject qo, Model model){
        //1.根据发布时间排列
        qo.setOrderBy("t.releaseTime desc");
        //2.发布状态才查询出来
        qo.setState(Travel.STATE_RELEASE);
        model.addAttribute("pageInfo",travelService.query(qo));
        return "/travel/releaseList";
    }

    /**
     * 修改状态
     * @param travel
     * @return
     */
    @RequestMapping("changeState")
    @ResponseBody
    public JSONResult changeState(Travel travel){
        travelService.changeState(travel);
        return new JSONResult();
    }

    /**
     * 查询内容
     * @param id
     * @return
     */
    @RequestMapping("selectContentById")
    @ResponseBody
    public TravelContent selectContentById(Long id){
        return travelService.selectContentById(id);
    }

   

}
